package com.quyunshuo.module.home.activity.main

import android.Manifest
import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.RemoteViews
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.recyclerview.widget.GridLayoutManager
import com.quyunshuo.edwincoffeetwo.common.ui.BaseActivity
import com.quyunshuo.module.home.NotificationUtils
import com.quyunshuo.module.home.R
import com.quyunshuo.module.home.databinding.HomeActivityMainBinding
import com.quyunshuo.module.home.databinding.HomeActivityMainComposeBinding
import com.quyunshuo.module.home.dialog.TestDialog
import com.quyunshuo.module.home.util.AppExit
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainComposeActivity : BaseActivity<HomeActivityMainComposeBinding, HomeComposeViewModel>() {

    companion object {

        fun newInstance(context: Context) {
            context.startActivity(Intent(context, MainComposeActivity::class.java))
        }
    }



    override val mViewModel by viewModels<HomeComposeViewModel>()

    override fun createVB() = HomeActivityMainComposeBinding.inflate(layoutInflater)


    override fun HomeActivityMainComposeBinding.initView() {
        composeView.setContent {
            MainPage()
        }

    }

    override fun HomeActivityMainComposeBinding.initListener() {

    }

    override fun initObserve() {

    }

    override fun initRequestData() {
    }


}

/**
 * 现在要实现一个功能，点击往一个集合里面添加项目
 */
@Composable
fun MainPage(
){
    // 自动创建并获取 ViewModel 实例
    val viewModel: HomeComposeViewModel = viewModel()
    // 收集购物车列表状态（如果使用 StateFlow）
    val coffeeCarList = viewModel._coffeeCarList


    // 创建响应式总价
    val totalPrice by remember(coffeeCarList) {
        derivedStateOf {
            // 在这里计算总价，当 coffeeCarList 变化时会自动更新
            coffeeCarList.sumOf { it.price }
        }
    }

    Column {
        LazyVerticalGrid(
            modifier = Modifier.height(390.dp).padding(top = 50.dp),
            columns = GridCells.Fixed(2),
            horizontalArrangement = Arrangement.spacedBy(16.dp),
            verticalArrangement = Arrangement.spacedBy(16.dp),
            contentPadding = PaddingValues(16.dp)
        ) {
            items(viewModel.getAllList()){
                Column(
                    Modifier.background(colorResource(R.color.base_colorAccent), shape = RoundedCornerShape(8.dp))
                        .height(150.dp)
                        .clickable {
                            viewModel.addCoffeeCar(it)
                        }
                ){
                    Text(
                        text = it.name
                    )
                }
            }
        }
        Spacer(modifier =  Modifier.height(20.dp))
        Row (
            modifier = Modifier
                .fillMaxWidth().
                height(50.dp).
                background(colorResource(R.color.base_colorAccent), shape = RoundedCornerShape(100.dp)),
            verticalAlignment = Alignment.CenterVertically
        ){
            Text(
                text = "${coffeeCarList.size}",
                modifier = Modifier.padding(start = 10.dp).background(colorResource(R.color.main_tab1_inactive),
                    RoundedCornerShape(100.dp)
                ).padding(10.dp)
            )
            Spacer(Modifier.width(10.dp))
            Text(
                text = "$totalPrice",
                style = TextStyle(
                    fontSize = 30.sp,
                    fontWeight = FontWeight.Bold
                )
            )

        }
    }
}

@Preview
@Composable
fun MainPagePreview(){
    MainPage()

}


